package com.xpm.base;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;

import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseRecycleViewActivity <T,VB extends ViewBinding,Adapter extends BaseAdapter<T,? extends ViewBinding>> extends AppCompatActivity {
    protected VB binding;
    protected SmartRefreshLayout refreshLayout;
    protected RecyclerView recyclerView;
    protected final List<T> dataList  = new ArrayList<>();
    protected Adapter adapter;
    // 当前页码（可根据需要修改初始值）
    protected int currentPage = 1;
    protected static final int INITIAL_PAGE = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = getBinding();
        setContentView(binding.getRoot());
        initRefreshLayout();
        initRecyclerView();
    }
    /**
     *   抽象方法，子类需实现具体Binding类的初始化
     */
    private void initRefreshLayout() {
        refreshLayout = getRefreshLayout();
        refreshLayout.setOnRefreshListener(refresh -> onRefresh());
        refreshLayout.setOnLoadMoreListener(refresh -> onLoadMore());
    }
    private void initRecyclerView() {
        recyclerView = getRecyclerView();
        recyclerView.setLayoutManager(createLayoutManager());
        adapter = createAdapter();
        recyclerView.setAdapter(adapter);
        loadData();
    }
    /**
     *   抽象方法，子类需实现具体Binding类的初始化
     */
    protected abstract VB getBinding();
    protected abstract SmartRefreshLayout getRefreshLayout();
    protected abstract RecyclerView getRecyclerView();

    /**
     * 创建 数据适配器
     * @return
     */
    protected abstract Adapter createAdapter();
    /****
     * 加载数据的逻辑
     */
    protected abstract  void loadData();
    // 可选重写方法
    protected RecyclerView.LayoutManager createLayoutManager() {
        return new LinearLayoutManager(this);
    }
    // 刷新加载回调
    /**
     * 刷新逻辑
     */
    protected  void onRefresh(){
        resetPage(); // 刷新逻辑
        loadData();
    }
    /**
     * 加载逻辑
     */
    protected  void onLoadMore(){
        currentPage++;
        loadData();
    }
    // 刷新完成
    protected void finishRefresh(boolean success) {
        if(refreshLayout!=null)
          refreshLayout.finishRefresh(success);
    }

    // 加载完成
    protected void finishLoadMore(boolean success, boolean noMoreData) {
        if(refreshLayout != null)
            refreshLayout.finishLoadMore(100,success,noMoreData);
    }

    // 重置页码
    protected void resetPage() {
        currentPage = INITIAL_PAGE;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(recyclerView!=null){
            recyclerView.setAdapter(null);
            recyclerView.setLayoutManager(null);
        }
        binding = null;
    }

}
